﻿using PEPe.BusinessObject;
using PEPe.DataAccessModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;

using System.Threading;
using System.Threading.Tasks;

namespace PEPe.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        public ActionResult Index()
        {
            ModelState.AddModelError("", "");

            return View();
        }


        public ActionResult Logar(string login, string senha)
        {
            //ModelState.AddModelError("Login", "Error");
            LoginBO loginbo = new LoginBO();
            UsuarioLogadoModel user = loginbo.Login(login, senha);

            if (user != null && user.Logado)
            {
                Session["user"] = user;
                return RedirectToAction("Index", "Home", new { area = "Logado" });
            }
            else
            {
                ViewBag.Erro = "Login ou Senha Invalido!";
            }


            return View("Index");
        }


        public ActionResult Cadastrar()
        {
            string nome = Request.Form["nome"];
            string email = Request.Form["email"];
            string senha = Request.Form["senha"];

            UsuarioBO usrBO = new UsuarioBO();

            Usuario usuario = new Usuario();
            usuario.Nome = nome;
            usuario.Senha = senha;
            usuario.Email = email;

            usrBO.Salvar(usuario);

            //Disparo de e-mail
            try
            {

                MailMessage mailMessage = new MailMessage()
                {
                    Subject = "Cadastro PEPe",
                    Body = "Seus dados foram cadastrados para confirmar seu cadastro clique <a href=''>Aqui</a>.",
                    IsBodyHtml = false
                };
                mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["emailFrom"]);
                mailMessage.To.Add(new MailAddress(email));
                SmtpClient smtpClient = new SmtpClient();

                smtpClient.Send(mailMessage);




            }
            catch (Exception ex)
            {
                //Response.Write(ex.Message);
            }


            return View("Index");
        }

    }
}